<?php

$rootdir = '../';
include_once('session.php');

global $desql;
$desql->Connect();

function CreateCountry($Nom, $FraisPort)
{
	global $desql;
	
	$result = $desql->Query('INSERT INTO pays(nom, fraisport) 
										VALUES(\''. mysql_real_escape_string($Nom) . '\',
												\''. mysql_real_escape_string($FraisPort) . '\'
												)');
	return $result;
}

function GetAllCountries()
{
	global $desql;
	$countries = array();
	
	$answer = $desql->Query('SELECT * FROM pays');
	if($answer == false)
		return false;
		
	while($country = mysql_fetch_array($answer))
		$countries[$country['id']] = $country;
	
	return $countries;
}

function DeleteCountryById($Id)
{
	global $desql;
	
	if(is_numeric($Id) == false)
		return false;
	else
		return $desql->Query('DELETE FROM pays WHERE id=' . $Id);
}

function GetFraisportByCountry($Country)
{
	global $desql;
	
	$result = $desql->Query('SELECT * FROM pays WHERE nom=\'' . mysql_real_escape_string($Country) . '\'');
	
	if($result == false)
		return false;
		
	if($t = mysql_fetch_array($result))
		return $t;
	else
		return false;
}

function GetAdminCountriesHTMLContent()
{
	$HTML = '<div style="margin-top: 20px; margin-left: 20px;">';
		
	$HTML .= '
		<table style="font-size: 80%; width: 90%; text-align: center;">
			<tr>
				<th style="border: 1px solid gray;">Nom</th>
				<th style="border: 1px solid gray;">Frais de port (€)</th>
				<th style="border: 1px solid gray;">Options</th>
			</tr>
			<tr>
				<td  style="border: 1px solid gray;"></td>
				<td  style="border: 1px solid gray;"></td>
				<td  style="border: 1px solid gray;"><div id="icone-nouveau"><a href="cadre_admin.php?type=5&new=1"></a></div></td>
			<tr>' . GetAdminCountryHTMLContent() . '
		</table>
	';
	return $HTML . '</div>';
}

function GetAdminCountryHTMLContent()
{
	$countries = GetAllCountries();
	$HTML = '';
	
	if($countries == false)
		return '';
	
	foreach($countries as $country)
	{
		$HTML .= '
			<tr>
				<td style="border: 1px solid gray;">' . $country['nom'] . '</td>
				<td style="border: 1px solid gray;">' . $country['fraisport'] . '</td>
				<td style="border: 1px solid gray;">
					<div id="icone-supprimer">
						<a href="cadre_admin.php?type=5&deleteid=' . $country['id'] . '" onClick="return(confirm(\'Etes-vous sûr de vouloir supprimer ce pays?\'));"></a>
					</div>
				</td>
			</tr>
		';
	}

	return $HTML;
}

function GetCreateCountryForm()
{
	$HTML = '
		<h4>Nouveau pays</h4>
		<form style="margin-left: 20px; margin-top: 20px; font-size: 80%;" method="post">
			<strong style="margin-top: 20px; margin-left: 20px;">
				Ne pas mettre d\'apostrophe dans le nom du pays.
			</strong><br /><br />
			<table style="margin: auto;">
				<tr>
					<td style="width: 150px;"><label for="nom">Nom :</label></td>
					<td><input type="text" tabindex="1" name="nom" size="15" /><br /></td>
				</tr>
				<tr>
					<td style="width: 150px;"><label for="fraisport">Frais de port en €:</label></td>
					<td><input type="text" tabindex="2" name="fraisport" size="15" /><br /></td>
				</tr>
				<tr>
					<td></td>
					<td><br /><input type="submit" name="send" tabindex="3" value="Envoyer" /></td>
				</tr>
			</table>
		</form>
	';
	
	return $HTML;
}

?>